Integer Extensions - 1st, 2nd, 3rd etc [closed]

Posted by David Schiefer on Stack Overflow See other posts from Stack Overflow or by David Schiefer
Published on 2010-07-23T16:35:34Z Indexed on 2011/01/08 5:53 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

Possible Duplicate:
NSNumberFormatter and ‘th’ ‘st’ ‘nd’ ‘rd’ (ordinal) number endings

Hello,

I'm building an application that downloads player ranks and displays them. So say for example, you're 3rd out of all the players, I inserted a condition that will display it as 3rd, not 3th and i did the same for 2nd and 1st. When getting to higher ranks though, such as 2883rd, it'll display 2883th (for obvious reasons)

My question is, how can I get it to reformat the number to XXX1st, XXX2nd, XXX3rd etc?

To show what I mean, here's how I format my number to add a "rd" if it's 3

if ([[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@"3"])
{
    NSString*badge = [NSString stringWithFormat:@"%@rd",[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSString*scoreText = [NSString stringWithFormat:@"ROC Server Rank: %@rd",[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    profile.badgeValue = badge;
    rank.text = scoreText;
}

I can't do this for every number up to 2000 (there are 2000 ranks in total) - what can I do to solve this problem?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa